import os
import requests

'''
  <script crossorigin src="https://cdn.jsdelivr.net/npm/react@18/umd/react.development.js"></script>
  <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.development.js"></script>

  <!-- 引入 Babel 用于编译 JSX -->
  <script src="https://cdn.jsdelivr.net/npm/babel-standalone@6/babel.min.js"></script>

  <!-- 引入 React Flow 相关依赖 -->
  <script src="https://cdn.jsdelivr.net/npm/reactflow@11.11.4/dist/umd/index.min.js"></script>
  ,
    {
        'url': 'https://unpkg.com/browse/@antv/x6@1.26.2/dist/x6.js',
        'filename': 'x6.js'
    }
  '''

# 定义要下载的文件 URL 和保存路径
files_to_download = [
   {
        'url': 'https://cdn.jsdelivr.net/npm/react@18/umd/react.development.js',
        'filename': 'react.development.js'
    },
    {
        'url': 'https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.development.js',
        'filename': 'react-dom.development.js'
    },
    {
        'url': 'https://cdn.jsdelivr.net/npm/babel-standalone@6/babel.min.js',
        'filename': 'babel.min.js'
    },
    {
        'url': 'https://unpkg.com/babel-standalone@6/babel.min.js',
        'filename': 'babel.min.js'
    },
    {
        'url': 'https://cdn.jsdelivr.net/npm/reactflow@11.11.4/dist/umd/index.min.js',
        'filename': 'reactflow.min.js'
    }
]

# 保存文件的目录
output_dir = 'js'

# 确保输出目录存在
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 下载文件
for file in files_to_download:
    url = file['url']
    filename = file['filename']
    filepath = os.path.join(output_dir, filename)

    print(f'Downloading {url} to {filepath}')

    try:
        response = requests.get(url)
        response.raise_for_status()  # 检查请求是否成功

        with open(filepath, 'wb') as f:
            f.write(response.content)
        print(f'Successfully downloaded {filename}')
    except requests.exceptions.RequestException as e:
        print(f'Failed to download {filename}: {e}')

print('All files have been downloaded.')